/**
 * 
 */
package com.kylin.erp.oa.system.controller;

import javax.servlet.http.HttpSession;

import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.kylin.erp.dbmodel.TMembers;
import com.kylin.frame.core.BaseController;
import com.kylin.frame.util.ContantsUtil;
import com.kylin.frame.util.encrypt.TripleDes;
import com.kylin.frame.web.SessionMember;

/**
 * 提供登录、退出等常用action
 * 
 * @author huxiang
 * 
 */

public class CommonController extends BaseController {

	@ClearInterceptor(ClearLayer.ALL)
	public void index() {

		this.redirect("/login");
	}

	@ClearInterceptor(ClearLayer.ALL)
	public void login() {

		this.render("login.html");
	}

	@ClearInterceptor(ClearLayer.ALL)
	public void doLogin() {
		
		String username = this.getPara("username_");
		String password = this.getPara("password_");
		TMembers m = new TMembers().findFirst(
				"select * from t_members where username_ = ?", username);
		if (null == m) {
			this.renderJson("msg", "用户名：" + username + "不存在");
			return;
		}
		if (!password.equals(TripleDes.decrypt(m.getStr("password_"), null))) {
			this.renderJson("msg", "密码错误");
			return;
		}
		if ("Y".equals(m.get("locked_"))) {
			this.renderJson("msg", "该用户已经被锁定，无法登录");
			return;
		}
		SessionMember sm = new SessionMember();
		sm.setMember(m);

		this.getSession().setAttribute(ContantsUtil._SESSIONOB, sm);
		this.renderJson("msg", "");
	}

	public void frame() {

		this.render("frame.html");
	}

	public void logout() {

		HttpSession s = this.getSession(false);
		if (null != s) {
			s.setAttribute(ContantsUtil._SESSIONOB, null);
			// s.invalidate();
		}
		this.renderJson("msg", "成功退出");
	}
}
